package ViewOption;

import Pojo.Bank;
import Service.BankService;
import Utils.BaseMessage;
import org.apache.log4j.Logger;
import org.omg.CORBA.OBJECT_NOT_EXIST;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;

public class bankVo {
    private final BankService bankService = new BankService();
    private final Logger logger = Logger.getLogger(bankVo.class);

    /**
     * 获取所有银行数据
     *
     * @return
     */
    public Object getBankMsgAll() {
        try {
            return new BaseMessage(0, null, bankService.getBankMsgAll());
        } catch (SQLException throwables) {
            logger.error(throwables.getMessage());
            return new BaseMessage(3, "出现异常请联系管理员处理", null);
        }
    }

    /**
     * 添加银行
     *
     * @param request
     * @return
     */
    public Object addBankIsAdmin(HttpServletRequest request) {
        String bankName = request.getParameter("bankName");
        try {
            if (bankService.addBankIsAdmin(bankName) < 1) {
                return new BaseMessage(2, "添加失败,请查看日志报错信息", null);
            }
        } catch (SQLException throwables) {
            logger.error(throwables.getMessage());

            return new BaseMessage(3, "出现异常", null);
        }
        return new BaseMessage(0, null, "添加成功");
    }

    /**
     * 修改银行名称
     *
     * @param request
     * @return
     */
    public Object updateBankIsAdminByBankId(HttpServletRequest request) {
        String bankName = request.getParameter("bankName");
        String bankId = request.getParameter("bankId");
        try {
            if (bankService.updateBankMsgIsAdminByBankId(bankName, bankId) < 1) {
                return new BaseMessage(2, "修改失败", null);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return new BaseMessage(3, "出现异常,请查看日志处理", null);
        }
        return new BaseMessage(0, null, "修改成功");
    }

    /**
     * 删除银行内容
     *
     * @param request
     * @return
     */
    public Object deleteBankIsAdminByBankId(HttpServletRequest request) {
        String bankId = request.getParameter("bankId");
        try {
            if (bankService.deleteBankIsAdminByBankId(bankId) < 1) {
                return new BaseMessage(2, "删除失败", null);
            }
        } catch (SQLException throwables) {
            logger.error(throwables.getMessage());
            return new BaseMessage(3, "出现异常,请查看日志", null);
        }
        return new BaseMessage(0, null, "删除成功");
    }
}
